import 'package:flutter/material.dart';
import 'package:flutter_ytx_plugin/src/utils/screen_utils.dart';
import 'package:flutter_ytx_plugin/src/widget/view/videoScreen.dart';
import 'package:get/get.dart';
import 'package:video_player/video_player.dart';

class VideoWidget extends StatefulWidget {
  final String url;
  final double aspectRatio;
  final bool isPlaying;
  final bool isLooping;
  bool? isMaxPlayer;
  Function? function;

  VideoWidget(
      {Key? key,
      required this.url,
      required this.aspectRatio,
      this.isPlaying = false,
      this.isLooping = false,
      this.function,
      this.isMaxPlayer = false})
      : super(key: key);

  @override
  State<VideoWidget> createState() => _VideoWidgetState();
}

class _VideoWidgetState extends State<VideoWidget> {
  late double _aspectRatio;
  final _isPlaying = false.obs;
  late VideoPlayerController _controller;
  bool isLoading = true;
  double _progressValue = 0.0;

  @override
  void initState() {
    _aspectRatio = widget.aspectRatio;
    _isPlaying.value = widget.isPlaying;

    super.initState();
    _simulateLoadingProcess();
    _controller = VideoPlayerController.networkUrl(Uri.parse(widget.url))
      ..addListener(() {
        _isPlaying.value = _controller.value.isPlaying;
      })
      ..initialize().then((_) {
        if (widget.isPlaying) {
          _controller.play();
        }
        _controller.setLooping(widget.isLooping);
        _isPlaying.value = _controller.value.isPlaying;
        _controller.seekTo(const Duration(seconds: 1));
      });
  }

  void _simulateLoadingProcess() async {
    for (int i = 0; i <= 100; i++) {
      await Future.delayed(const Duration(milliseconds: 2));
      setState(() {
        _progressValue = i / 100.0;
        if (i == 100.0) {
          isLoading = false;
        }
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => Stack(
        children: [
          AspectRatio(
            aspectRatio: _aspectRatio,
            child: GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: _togglePlayPause,
              child: isLoading
                  ? Center(
                      child: Container(
                        width: 24.wpx,
                        height: 24.wpx,
                        alignment: Alignment.center,
                        child: CircularProgressIndicator(
                          value: _progressValue,
                          strokeWidth: 2.wpx,
                          backgroundColor: Colors.grey[300],
                          valueColor:
                              const AlwaysStoppedAnimation<Color>(Colors.blue),
                        ),
                      ),
                    )
                  : VideoPlayer(_controller),
            ),
          ),
          Visibility(
            visible: !_isPlaying.value,
            child: Positioned.fill(
              child: GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: _togglePlayPause,
                child: Container(
                  height: 28..wpx,
                  width: 28.wpx,
                  child: Center(
                    child: Icon(
                      _isPlaying.value
                          ? Icons.pause_circle_filled
                          : Icons.play_circle_outline,
                      size: 28.wpx,
                      color: Colors.white,
                    ),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  void _togglePlayPause() {
    if (widget.function != null) {
      widget.function?.call();
      return;
    }
    if (widget.isMaxPlayer ?? false) {
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => VideoScreen(
            url: widget.url,
            aspectRatio: _controller.value.aspectRatio ?? 1,
          ),
        ),
      );
    } else {
      _isPlaying.value = !_isPlaying.value;
      if (_isPlaying.value) {
        _controller.play();
      } else {
        _controller.pause();
      }
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}
